#include "MainHeader.h"

int cToolset::InitTileset(HWND setup){
hwndBound=setup;
int Shit=0;
	// Fill in the window class structure with parameters 
	// that describe the main window. 
	RECT poo;
	wcTileset.cbSize = sizeof(wcTileset);		   // size of structure 
	wcTileset.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	wcTileset.lpfnWndProc = TilesetProc;	  // points to window procedure 
	wcTileset.cbClsExtra = NULL;				// no extra class memory 
	wcTileset.cbWndExtra =NULL; 			   // no extra window memory 
	wcTileset.hInstance = hGlobal;		   // handle to instance 
	wcTileset.hIcon = NULL; 			 // predefined app. icon 
	wcTileset.hCursor = NULL;				// predefined arrow 
	// wcTileset.hbrBackground = GetStockObject( WHITE_BRUSH);				   // white background brush 
	wcTileset.lpszMenuName =  NULL;    // name of menu resource 
	wcTileset.lpszClassName = "TilesetClass";  // name of window class 
	Shit=GetLastError();
	
	// Register the window class. 
	
	RegisterClassEx(&wcTileset); 
	//Create the Window
	 Shit=GetLastError();
   
   GetWindowRect(hwndBound,&poo);
	hwndTileset = CreateWindow("TilesetClass",NULL, WS_CHILD| WS_VISIBLE,poo.left ,poo.top + 3,256,256,hwndMain,0,hGlobal,0);

    vScroll.maxscroll=100;
	if(vScroll.MakeMe(SBS_VERT,hwndMain, IDC_VSCROLLTILESET)) 
		MessageBox(0,"We have a problem", "None", MB_OK);
	imgTileset.Create(1024, 1024);
	bbTileset.Create(1024, 1024);
	bbTileset.Clear();
	ShowWindow(hwndTileset,SW_SHOW);

return 0;
}

LRESULT CALLBACK TilesetProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
    RECT rTileset;
	switch (message) 
	{

		 
	
	case WM_PAINT:
		
		hdc = BeginPaint(hWnd, &ps);

		GetWindowRect(hWnd,&rTileset);
		BitBlt(hdc, 0,0,rTileset.right-rTileset.left,rTileset.bottom-rTileset.top,imgTileset.DC(),0,Tools.vScroll.curscroll*16,SRCCOPY);
		EndPaint(hWnd, &ps);
		
		break;
	case WM_VSCROLL:	// exact same idea, but V scroll instead of H scroll
		
	    break;
	case WM_HSCROLL:
		
		break;
	case WM_LBUTTONDOWN:
        
	        Tools.GrabTile(GetX(lParam),GetY(lParam));
            return 0;
	
		break;
	case WM_RBUTTONDOWN:
		break;
	case WM_RBUTTONUP:
		
	
		break;
	case WM_LBUTTONUP:
		
		break;
	case WM_KEYDOWN:							// Here's what you're interested in! -- check to see if someone pressed a key.
	

		break;
	case WM_MOUSEMOVE:
		
		
		break;
	case WM_SIZE:
		
		
		break;
	case WM_DESTROY:
		
		break;
		

		
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

int DrawBlock(short Map16n , int destX , int destY){
 
	    imgTileset.Draw(clsGame.TileImage,(destX), (destY), (short)clsGame.TSA.nTSA[Map16n]);
		imgTileset.Draw(clsGame.TileImage,(destX) + 8, (destY), (short)clsGame.TSA.nTSA[(Map16n)+1]);
		imgTileset.Draw(clsGame.TileImage,(destX), (destY) + 8, (short)clsGame.TSA.nTSA[Map16n+2]);
		imgTileset.Draw(clsGame.TileImage,(destX) + 8, (destY) + 8, (short)clsGame.TSA.nTSA[Map16n+3]);
	
		return 0;
}
int cToolset::DrawTileset(){
    int k=0;
    int i=0;
	int TC=0;

	
 
  
	imgTileset.Clear();
	imgTileset.SetPalette(&clsGame.PcPalMem[0]);

    
	for( i= 0; i<clsGame.TSA.max;i++)
	{
      
		     DrawBlock( i*4, (i % 16) * 16, ((i / 16)) * 16);


    }
    vScroll.curscroll = 0;
    InvalidateRect(hwndTileset,0,1);
    return 0;
} 

int cToolset::GrabTile(int X, int Y){
int tX=X/16, tY=Y/16;
    

return Tile=(tY+vScroll.curscroll)*16 + tX;
};